package com.example.jin.myapplication;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.example.jin.myapplication.beatbox.BeatBoxFragment;

import java.util.UUID;

public class CrimeListActivity extends SingleFragmentActivity implements CrimeListFragment.Callback,CrimeFragment.Callback{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_masterdetail;
    }

    @Override
    protected Fragment createFragment() {
        CrimeListFragment fragment=new CrimeListFragment();
        return fragment;
    }
    //使用callback函数关联Activity和Fragment，Fragment必须要做到独立，Activity可以看做是Fragment的Boss
    @Override
    public void onItemClicked(Crime crime) {
        UUID uuid=crime.getId();
        FragmentManager manager=getSupportFragmentManager();
        if(findViewById(R.id.detail)==null){
            Intent intent=new Intent(CrimeListActivity.this,CrimePagerActivity.class);
            intent.putExtra("crimeid",uuid);
            startActivityForResult(intent,1);
        }
        else{
            Log.i("main","replace fragment");
            Fragment detailfragment=CrimeFragment.build(uuid);
            manager.beginTransaction().replace(R.id.detail,detailfragment).commit();
        }
    }
    //当DetailFragment发生编辑变化时，需要更新List中的数据
    @Override
    public void onItemUpdate(Crime c) {
        FragmentManager manager=getSupportFragmentManager();
        CrimeListFragment crimeListFragment=(CrimeListFragment)manager.findFragmentById(R.id.framelayout);
        if(crimeListFragment!=null){
            crimeListFragment.updateUI();
        }
    }

/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
*/
}
